package view.common.search;

import java.util.ArrayList;
import java.util.List;

import oracle.jbo.ViewCriteria;
import oracle.jbo.ViewCriteriaItem;
import oracle.jbo.ViewCriteriaRow;
import oracle.jbo.ViewObject;

public class AdvancedSearchGroup extends AdvancedSearch implements AdvancedSearchConst {
    ViewCriteria vc;
    private int conjunction;
    private List<AdvancedSearchItem> listChildren;

    public AdvancedSearchGroup() {
        super();
        conjunction = VC_CONJ_AND;
        listChildren = new ArrayList();
    }

    public static void setViewCriteria(List<AdvancedSearchGroup> listAdvancedSearchGroup,
                                       ViewCriteria viewCriteria) {
        List<AdvancedSearch> listProcess = new ArrayList();
        for (AdvancedSearchGroup asg : listAdvancedSearchGroup) {
            asg.vc = viewCriteria;
        }
        listProcess.addAll(listAdvancedSearchGroup);
        while (listProcess.size() > 0) {
            List<AdvancedSearch> listProcessNext = new ArrayList();
            for (AdvancedSearch as : listProcess) {
                if (as instanceof AdvancedSearchGroup) {
                    AdvancedSearchGroup asg = (AdvancedSearchGroup)as;
                    ViewCriteriaRow vcr = asg.vc.createViewCriteriaRow();
                    asg.vc.insertRow(vcr);
                    vcr.setConjunction(asg.getConjunction());
                    for (AdvancedSearchItem asiChild :
                         asg.getListChildren()) {
                        asiChild.vcr = vcr;
                        listProcessNext.add(asiChild);
                    }
                }
                if (as instanceof AdvancedSearchItem) {
                    AdvancedSearchItem asi = (AdvancedSearchItem)as;
                    if (OPER_EXISTS.equals(asi.getOperator())) {
                        ViewCriteriaItem vci =
                            asi.vcr.ensureCriteriaItem(asi.getItemName());
                        vci.setConjunction(asi.getConjunction());
                        vci.setRequired(asi.getRequired());
                        vci.setOperator(asi.getOperator());
                        vci.setUpperColumns(asi.isUpperColumns());
                        ViewObject vo =
                            vci.getAccessorVO(asi.vcr.getViewCriteria().getViewObject());
                        ViewCriteria vc = vo.createViewCriteria();
                        vc.setParent(vci);
                        for (AdvancedSearchGroup asgChild :
                             asi.getListChildren()) {
                            asgChild.vc = vc;
                            listProcessNext.add(asgChild);
                        }
                        vci.setValue(vc);
                    } else {
                        ViewCriteriaItem vci =
                            asi.vcr.ensureCriteriaItem(asi.getItemName());
                        vci.setConjunction(asi.getConjunction());
                        vci.setRequired(asi.getRequired());
                        vci.setOperator(asi.getOperator());
                        vci.setUpperColumns(asi.isUpperColumns());
                        if (asi.getListValue().size() == 1) {
                            vci.setValue(asi.getListValue().get(0));
                        } else if (asi.getListValue().size() > 1) {
                            int listCount = 0;
                            for (Object obj : asi.getListValue()) {
                                vci.setValue(listCount++, obj);
                            }
                            vci.setValueMinCardinality(listCount);
                            vci.setValueMaxCardinality(listCount);
                        }
                    }
                }
            }
            listProcess = listProcessNext;
        }
    }

    public void setConjunction(int conjunction) {
        this.conjunction = conjunction;
    }

    public int getConjunction() {
        return conjunction;
    }

    public List<AdvancedSearchItem> getListChildren() {
        return listChildren;
    }
}
